#include "comm.hpp"

int main()
{
    //创建共享的key值
    key_t key = ftok(PATH_NAME,PROJ_ID);
    if(key < 0)
    {
        log("create key faile",Error) << " client key: " << key << endl;
    }
    log("create key success",Debug) << " client key: " << key << endl;

    //server中创建了共享内存 所以直接获取共享内存
    int shmid = shmget(key, SHM_SIZE, 0);
    if(shmid < 0)
    {
        log("get shm faile",Error) << " shmid: " << shmid << endl;
        exit(1);
    }
    log("get shm success",Debug) << " shmid: " << shmid << endl;
    // sleep(10);

    //将指定的共享内存，挂接到自己的进程地址空间
    char* shmaddr = (char*)shmat(shmid,nullptr,0);
    if(shmaddr == nullptr)
    {
        log("attach shm faile",Error) << " shmid: " << shmid << endl;
        exit(1);
    }
    log("attach shm success",Debug) << " shmid: " << shmid << endl;
    // sleep(10);

    char ch = 'a';
    for(; ch <= 'z';ch++)
    {
        snprintf(shmaddr,SHM_SIZE - 1,"hello server , i am client process , my pid is %d, my char %c", getpid(),ch);
        sleep(1);
    }

    //将共享内存与进程地址空间去关联
    int n = shmdt(shmaddr);
    assert(n != -1);
    (void)n;
    log("detach shm success",Debug) << " shmid: " << shmid << endl;
    
    
    return 0;
}